package com.syas.service.services;

import com.syas.base.service.services.ChainEntityService;
import com.syas.common.exception.EntityOperateException;
import com.syas.common.exception.ValidatException;
import com.syas.common.extension.StringHelper;
import com.syas.common.utilities.PageList;
import com.syas.common.utilities.PageListUtil;
import com.syas.dao.interfaces.IAttendInfoDao;
import com.syas.model.models.AttendInfo;
import com.syas.service.interfaces.IAttendInfoService;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("AttendInfoService")
public class AttendInfoService extends ChainEntityService<Integer, AttendInfo, IAttendInfoDao> implements IAttendInfoService{

    @Autowired
    public AttendInfoService(@Qualifier("AttendInfoDao") IAttendInfoDao attendInfoDao){
        super(attendInfoDao);
    }
    @Override
    @SuppressWarnings("unchecked")
    public PageList<AttendInfo> listPage(String name,
                                    String certid,
                                    int pageNo,
                                    int pageSize) {

		Criteria countCriteria = entityDao.getCriteria();
		Criteria listCriteria = entityDao.getCriteria();
		if(!StringHelper.isEmpty(name)){
			countCriteria.add(Restrictions.eq("name", name));
    		listCriteria.add(Restrictions.eq("name", name));
		}
        if(!StringHelper.isEmpty(certid)){
            countCriteria.add(Restrictions.eq("certId", certid));
            listCriteria.add(Restrictions.eq("certId", certid));
        }
        listCriteria.setFirstResult((pageNo-1)*pageSize);
        listCriteria.setMaxResults(pageSize);
        List<AttendInfo> items = listCriteria.list();
        countCriteria.setProjection(Projections.rowCount());
        Integer count=StringHelper.convertStringToInt(countCriteria.uniqueResult());
        return PageListUtil.getPageList(count, pageNo, items, pageSize);
    }
    public void delete() throws EntityOperateException, ValidatException {
        entityDao.delete();
    }
}
